﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;

public partial class User_ExperienceReg_experiencePersonal : System.Web.UI.Page
{
    UserBean bllUser = new UserBean();
    AccountsCharge bllAccount = new AccountsCharge();
    ReadParamtes bllParam = new ReadParamtes();
    DonatePoints2UserAccount bllPoints = new DonatePoints2UserAccount();
    SendSMS bllSMS = new SendSMS();
    WebUtility utility = new WebUtility();
    ConsultationCardPwdSelect bllCardInfo = new ConsultationCardPwdSelect();

    protected void Page_Load(object sender, EventArgs e)
    {
        this.txt_Birth.Attributes.Add("onfocus", "WdatePicker({skin:'ext',dateFmt:'yyyy-MM-dd'})");
        if (!IsPostBack)
        {
            WebUtility.BindList("行业属性", ddl_Vocation);
            WebUtility.BindList("文化程度代码", ddl_Education);
        }
    }

    protected void btn_Submit_Click(object sender, ImageClickEventArgs e)
    {
        string mobileNO = this.MobileNO.Text;
        string name = this.realName.Text;
        string sex = this.rb_Man.Checked ? rb_Man.Text : rb_Woman.Text;
        string phoneNO = this.phone.Text;
        string email = this.email.Text;
        string vocation = this.ddl_Vocation.SelectedIndex != 0 ? this.ddl_Vocation.SelectedItem.Text : "";
        string address = this.txt_Address.Text;
        string zipcode = this.txt_ZipCode.Text;
        string education = this.ddl_Education.SelectedIndex != 0 ? this.ddl_Education.SelectedItem.Text : "";
        string birth = this.txt_Birth.Text;
        string like = this.txt_Like.Text;
        string password = this.password.Text;

        bool flag = bllUser.IsExistsUserInfo(mobileNO);
        if (flag == true)
        {
            utility.AlertMsg("您的用户名已经被占用！");
            return;
        }
        else
        {
            bool isRequestTimeOut = bllCardInfo.IsRequestTimedOut(mobileNO, password);
            if (isRequestTimeOut == false)
            {
                utility.AlertMsg("对不起，您所输入的密码已过期！请重新申请。");
                return;
            }
            else
            {
                bool isSame = bllCardInfo.IsSameToVerifyCodeAndMobileNO(mobileNO, password);
                if (isSame == false)
                {
                    utility.AlertMsg("对不起，您所输入的验证码错误！请重新输入。");
                    return;
                }
                else
                {
                    //取得注册协议名称
                    string protocolName = "";
                    DataTable dtProtocol = bllParam.getParamInfo("RegProtocol");
                    for (int i = 0; i < dtProtocol.Rows.Count; i++)
                    {
                        protocolName += dtProtocol.Rows[i]["ParameterValue"].ToString() + ",";
                    }

                    int index1 = bllAccount.AddChargeAccount(Guid.NewGuid().ToString(), mobileNO, 0, "", password, protocolName.Trim(','), "3", 1);

                    if (index1 > 0)
                    {
                        int index = bllUser.AddContact_Experience(Guid.NewGuid().ToString(), mobileNO, name, sex, phoneNO, email, vocation, address, zipcode, education, birth, like);

                        if (index > 0)
                        {
                            string cardPoints = bllParam.getParamValue("donatePoints").ToString();
                            int index2 = bllPoints.donatePoints(Convert.ToInt32(cardPoints), mobileNO);

                            if (index2 == 1)
                            {
                                string regTemp = bllParam.getParamValue("SmsExperienceReg").ToString();
                                string regMsg = WebUtility.GenrateSendPurchaseRegMsg(mobileNO, password, cardPoints, regTemp);
                                bllSMS.SendMsg(regMsg, mobileNO, "1");
                                utility.AlertMsg("注册成功！,系统稍后将会以短信方式把用户名和密码发送到你的注册手机上");
                                Response.Redirect(WebUtility.getWebRootPath() + "/Login/Login.aspx");
                                return;
                            }
                            else
                            {
                                utility.AlertMsg("注册失败！");
                            }
                        }
                    }
                    else
                    {
                        utility.AlertMsg("注册失败！");
                    }
                }
            }
        }
    }
}
